<template>
  <div class="Cangkushezhi">
    <div class="sTop">
      &nbsp;&nbsp;&nbsp;&nbsp;仓库：<el-select
        v-model="valueRepository"
        size="small"
        placeholder="请选择"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      &nbsp;&nbsp;&nbsp;&nbsp;库位：<el-select
        v-model="valuePosition"
        size="small"
        placeholder="请选择"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      &nbsp;&nbsp;&nbsp;&nbsp;物料名称：<el-input
        placeholder="请输入物料名称"
        v-model="input"
        size="small"
        clearable
      ></el-input>
      &nbsp;<el-button size="mini" icon="el-icon-search" class="button1 addUser"
        >查询</el-button
      >
      <el-button size="mini" icon="el-icon-refresh-right" class="button1 detail"
        >重置</el-button
      >
    </div>

    <el-table
      :data="tableData"
      border
      :row-style="{ height: '60px', padding: '0 0' }"
      :cell-style="{ height: '30px', padding: '0 0' }"
      style="width: 100%"
    >
      <el-table-column align="center" type="selection"> </el-table-column>
      <el-table-column prop="firstLocation" label="原位置" align="center">
      </el-table-column>
      <el-table-column
        prop="lastLocation"
        label="移库后位置"
        align="center"
        width="200"
      >
      </el-table-column>
      <el-table-column prop="materialNumber" label="物料编码" align="center">
      </el-table-column>
      <el-table-column prop="materialName" label="物料名称" align="center">
      </el-table-column>
      <el-table-column
        prop="materialNum"
        label="数量"
        align="center"
        width="120"
      >
      </el-table-column>
      <el-table-column prop="unit" label="单位" align="center" width="90">
      </el-table-column>
      <el-table-column prop="initTime" label="记录时间" align="center">
      </el-table-column>
      <el-table-column label="操作" align="center" width="200">
        <template slot-scope="scope">
          <!-- <el-button
            class="addUser"
            size="mini"
            @click="handleEdit(scope.$index, scope.row)"
            >修改</el-button
          > -->
          <el-button
            class="rePassword"
            size="mini"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
          <!-- <el-button type="text" @click="handleChange(scope.$index, scope.row)">{{tableData[scope.$index].status=='正常'?'冻结':'正常'}}</el-button>  -->
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <el-pagination
      :page-size="pageShow"
      :current-page="pageNow"
      layout="prev, pager, next,sizes"
      :page-sizes="[2, 3, 5, 10]"
      @current-change="pageChange"
      @size-change="sizeChange"
      @prev-click="back()"
      @next-click="next()"
      :total="pageNumber"
    >
    </el-pagination>
    <!-- 添加新用户 -->
    <el-dialog
      title="添加新用户"
      :visible.sync="centerDialogVisible2"
      width="30%"
      center
    >
      <el-form
        :model="insertOneArr"
        ref="insertOneArr"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="username"
            v-model="insertOneArr.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            type="password"
            v-model="insertOneArr.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="nickname">
          <el-input
            type="nickname"
            v-model="insertOneArr.nickname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <div class="sexselect">
          <el-radio v-model="insertOneArr.sex" label="男">男</el-radio>
          <el-radio v-model="insertOneArr.sex" label="女">女</el-radio>
        </div>
        <el-form-item label="部门" prop="partId">
          <el-select v-model="insertOneArr.partId" placeholder="请选择">
            <el-option
              v-for="item in partArr"
              :key="item.partId"
              :label="item.partName"
              :value="item.partName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div class="datepicker">
          入职时间&nbsp;&nbsp;&nbsp;<el-date-picker
            v-model="insertOneArr.fitstTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
        <div class="datepicker">
          离职时间&nbsp;&nbsp;&nbsp;<el-date-picker
            v-model="insertOneArr.lastTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
        <el-form-item label="手机号码" prop="phoneNumber">
          <el-input
            type="phoneNumber"
            v-model.number="insertOneArr.phoneNumber"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="insertOneArr.status" placeholder="请选择用户状态">
            <el-option label="正常" value="1"></el-option>
            <el-option label="禁用" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input
            type="address"
            v-model="insertOneArr.address"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="addUser">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改用户信息 -->
    <el-dialog
      title="修改用户信息"
      :visible.sync="centerDialogVisible"
      width="30%"
      center
    >
      <el-form
        :model="managerInformation"
        ref="managerInformation"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="用户名" prop="username">
          <el-input
            type="username"
            v-model="managerInformation.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="员工姓名" prop="nickname">
          <el-input
            type="nickname"
            v-model="managerInformation.nickname"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <div class="sexselect">
          <el-radio v-model="managerInformation.sex" label="男">男</el-radio>
          <el-radio v-model="managerInformation.sex" label="女">女</el-radio>
        </div>
        <el-form-item label="部门" prop="partId">
          <el-select v-model="managerInformation.partId" placeholder="请选择">
            <el-option
              v-for="item in partArr"
              :key="item.partId"
              :label="item.partName"
              :value="item.partName"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <div class="datepicker">
          入职时间&nbsp;&nbsp;&nbsp;<el-date-picker
            v-model="managerInformation.fitstTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
        <div class="datepicker">
          离职时间&nbsp;&nbsp;&nbsp;<el-date-picker
            v-model="managerInformation.lastTime"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </div>
        <el-form-item label="手机号码" prop="phoneNumber">
          <el-input
            type="phoneNumber"
            v-model.number="managerInformation.phoneNumber"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="managerInformation.status"
            placeholder="请选择用户状态"
          >
            <el-option label="正常" value="1"></el-option>
            <el-option label="禁用" value="2"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="家庭住址" prop="address">
          <el-input
            type="address"
            v-model="managerInformation.address"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="update">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 删除用户对话框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <i class="el-icon-warning"></i><span>确认删除此条记录吗</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="deleteManager">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 改变用户状态对话框 -->
    <el-dialog title="提示" :visible.sync="centerDialogVisible3" width="30%">
      <span
        ><i type="waring" class="el-icon-warning"></i>确认修改用户状态吗</span
      >
      <span slot="footer" class="dialog-footer">
        <el-button @click="centerDialogVisible3 = false">取 消</el-button>
        <el-button type="primary" @click="changeStatus">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import repository from '@/api/repository.js';
import utils from '../../utils/utils';
export default {
  data() {
    return {
      valueRepository: '', //当前仓库选择的option
      valuePosition: '', //当前库位选择的option
      userStatus: '', //当前点击用户的状态status
      input: '',
      Userid: '', //删除用的用户id
      pageNow: 1 /* 当前页数 */,
      pageNumber: 50 /* 总页数 */,
      pageShow: 10 /* 每页显示个数 */,
      centerDialogVisible: false,
      dialogVisible: false,
      centerDialogVisible2: false,
      centerDialogVisible3: false,
      partArr: [], //部门数组
      managerArr: [], //修改用数组
      materialArr: [], //物料信息数组
      insertOneArr: {
        username: '',
        password: '',
        nickname: '',
        partId: '',
        fitstTime: '',
        lastTime: '',
        phoneNumber: '',
        address: ''
      }, //添加用对象
      tableData: [], //渲染到表格的数据
      options: [
        {
          //select 选项
          value: '1',
          label: '自家仓库'
        },
        {
          value: '2',
          label: '电力公司仓库'
        }
      ],
      value: '', //select当前选定的 option
      managerInformation: {}, //用于修改
      rules: {}
    };
  },
  created() {
    //获取物料列表
    repository.getMaterial(1, 20).then(res => {
      /* console.log(res); */
      this.materialArr = res.data.data.list;

      //获取记录列表
      repository.getTransfer(this.pageNow, this.pageShow).then(res => {
        /* console.log(res); */
        this.managerArr = res.data.data.list;

        for (let i = 0; i < this.managerArr.length; i++) {
          this.managerArr[i].initTime = utils.shiftDate(
            this.managerArr[i].initTime
          );
        }
        this.pageNumber = res.data.data.total;
        this.tableData = this.managerArr;
        this.useShiftMaterial();
      });
    });
  },
  /* mounted(){
      this.useShiftMaterial()
    }, */
  methods: {
    //pageSize改变
    sizeChange(i) {
      this.pageShow = i;
      repository.getMaterial(1, 20).then(res => {
        /* console.log(res); */
        this.materialArr = res.data.data.list;

        //获取记录列表
        repository.getTransfer(this.pageNow, this.pageShow).then(res => {
          /* console.log(res); */
          this.managerArr = res.data.data.list;
          for (let i = 0; i < this.managerArr.length; i++) {
            this.managerArr[i].initTime = utils.shiftDate(
              this.managerArr[i].initTime
            );
          }
          this.pageNumber = res.data.data.total;
          this.tableData = this.managerArr;
          this.useShiftMaterial();
        });
      });
    },
    //点击页码
    pageChange(i) {
      this.pageNow = i;
      repository.getMaterial(1, 20).then(res => {
        /* console.log(res); */
        this.materialArr = res.data.data.list;

        //获取记录列表
        repository.getTransfer(this.pageNow, this.pageShow).then(res => {
          /* console.log(res); */
          this.managerArr = res.data.data.list;
          for (let i = 0; i < this.managerArr.length; i++) {
            this.managerArr[i].initTime = utils.shiftDate(
              this.managerArr[i].initTime
            );
          }
          this.pageNumber = res.data.data.total;
          this.tableData = this.managerArr;
          this.useShiftMaterial();
        });
      });
    },
    //上一页
    back() {
      this.pageNow--;
    },
    //下一页
    next() {
      this.pageNow++;
    },
    /* 修改 */
    handleEdit(i, v) {
      this.centerDialogVisible = true;
      this.managerInformation = v;
    },

    /*修改更新数据*/
    update() {
      this.centerDialogVisible = false;
      yuangong.updateUser(this.managerInformation).then(res => {
        console.log(res);
        location.reload();
      });
    },
    /*删除管理员*/
    handleDelete(i, v) {
      this.dialogVisible = true;
      this.Userid = v.id;
    },
    /*删除管理员*/
    deleteManager() {
      this.dialogVisible = false;
      repository.deleteTransfer(this.Userid).then(res => {
        location.reload();
      });
    },
    //冻结 启用 用户
    handleChange(i, v) {
      this.centerDialogVisible3 = true;
      this.userStatus = v.id;
    },
    //冻结 启用 用户
    changeStatus() {
      yuangong.changeStatus(this.userStatus).then(res => {
        /* console.log(res); */
        location.reload();
      });
      this.centerDialogVisible3 = false;
    },
    //添加用户
    addUser() {
      this.centerDialogVisible2 = false;
      yuangong.addUser(this.insertOneArr).then(res => {
        /* console.log(res); */
        location.reload();
      });
    },
    //渲染物料信息
    shiftMaterial(id, mArr, tArr) {
      /* console.log(tArr,mArr); */
      for (let i = 0; i < tArr.length; i++) {
        for (let j = 0; j < mArr.length; j++) {
          /* console.log("ok"); */
          let emptyObj = {};
          if (id === mArr[j].materialId) {
            emptyObj = mArr[i];
            //console.log(emptyObj);
            Object.assign(tArr[i], emptyObj);
          }
        }
      }
    },
    //使用渲染物料函数
    useShiftMaterial() {
      for (let i = 0; i < this.tableData.length; i++) {
        /* console.log("ok1"); */
        this.shiftMaterial(
          this.tableData[i].materialId,
          this.materialArr,
          this.tableData
        );
      }
    }
  }
};
</script>

<style scoped>
* {
  margin: 0;
  padding: 0;
}
.Cangkushezhi {
  background-color: white;
}
.el-input {
  width: 200px;
  margin-right: 10px;
}
.sTop {
  padding-top: 20px;
  font-size: 14px;
  margin-bottom: 40px;
}
.el-table thead {
  background-color: #f5f7fa;
}
.has-gutter .is-leaf .cell {
  text-align: center;
  padding-left: 10px;
  width: 150px;
}
.el-pagination {
  margin-top: 20px;
  text-align: center;
}
.el-icon-warning {
  color: orange;
  font-size: 20px;
  margin-right: 5px;
}

.sTop >>> .button1 {
  text-align: center;
  width: 78px;
  height: 32px;
}
.sTop >>> .button2 {
  text-align: center;
  width: 118px;
  height: 32px;
}
.sTop >>> .button1,
.button2 {
  font-size: 14px;
}
.is-plain {
  width: 80px;
}
.dialog-footer .el-button--default,
.el-button--primary {
  width: 74px;
  height: 40px;
}
.datepicker {
  height: 40px;
  width: 405px;
  margin-bottom: 20px;
  padding-left: 30px;
}
.sexselect {
  width: 400px;
  height: 40px;
  padding-left: 100px;
}
.datepicker .el-date-editor.el-input,
.el-date-editor.el-input__inner {
  width: 200px;
}
.el-select >>> .el-input {
  width: 200px;
}
</style>
